package xiaobange.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import xiaobange.common.JacksonObjectMapper;

import java.util.List;
@Slf4j
@Configuration
public class WebMvcConFig extends WebMvcConfigurationSupport {


    /**
     * 拦截器
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
    }

    /**
     * 扩展框架消息转换器
     * @param converters
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器，底层使用Jackson将Java对象转换为JSON数据
        jsonMessageConverter.setObjectMapper(new JacksonObjectMapper());
        //将上面的消息转换器添加到MVC框架的消息转换器中
        converters.add(0,jsonMessageConverter);//这个索引就是让现在这个转换器排在前面，不然他会只用spring自带的转换器
    }

}
